
var currentPage = 1;

$(document).ready(function(){
	//外部js调用
    laydate({
        elem: '#startTime', //需显示日期的元素选择器
		event: 'focus', //触发事件
		format: 'YYYY-MM-DD hh:mm:ss', //日期格式
		istime: true, //是否开启时间选择
		isclear: true, //是否显示清空
		istoday: true, //是否显示今天
		issure: true, //是否显示确认
		festival: true, //是否显示节日
		min:'1900-01-01 00:00:00',//最小日期
		max:'2099-12-31 23:59:59',//最大日期
		start:laydate.now(),    //开始日期
		key:'2017-01-17 23:22:59'
		//choose: function(dates){} //选择好日期的回调
    });    
    //外部js调用
    laydate({
        elem: '#endTime', //需显示日期的元素选择器
		event: 'focus', //触发事件
		format: 'YYYY-MM-DD hh:mm:ss', //日期格式
		istime: true, //是否开启时间选择
		isclear: true, //是否显示清空
		istoday: true, //是否显示今天
		issure: true, //是否显示确认
		festival: true, //是否显示节日
		min:'1900-01-01 00:00:00',//最小日期
		max:'2099-12-31 23:59:59',//最大日期
		start:laydate.now(),    //开始日期
		key:'2017-01-17 23:22:59'
		//choose: function(dates){} //选择好日期的回调
    });

	init(currentPage,"","", "",callback);
})


	function init(currentPage,memberType,userType, phoneNumber,callback) {
		$("#thislist").html("");
		$.ajax({
			url: urlcore + "/api/muser/selectUsers?userType=" + userType + "&memberType=" + memberType + "&phone=" + phoneNumber + "&current=" + currentPage+"&startTime="+$('#startTime').val()+"&endTime="+$('#endTime').val(),
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				if(data.success == true) {
					callback(data.data);
						$("#pager").pager({
						pagenumber: currentPage,
						pagecount: data.data.pages,
						totalcount: data.data.total,
						buttonClickCallback: PageClick
					});

				} else if(data.code == 'OVERTIME') {
					var thisUrl = window.location.href;

					if(thisUrl.indexOf('login.html') <= -1) {
						top.window.location.href = "login.html";
					}

				} else {
					if(data.msg != '空数据') {
						alert(data.msg)
					} else {
						$('#thiscount').text(0);
					}
				}

			},
			error: function() {
				alert("error");
			}
		});
	}
	
	
	
var callback=function(data){
		
	$.each(data.list, function(i, n) {
		var status = '';
		if(n.muser.mStatus == 1) {
			mstatus = '正常';
		} else if(n.muser.mStatus == 2) {
			mstatus = '封号';
		}
		
		var membertype='';
		if(n.muser.memberType == 1) {
			membertype = '体验会员';
		} else if(n.muser.memberType == 2) {
			membertype = '彩虹会员';
		}
		
		var usertype='';
		if(n.muser.userType == 1) {
			usertype = '普通会员 ';
		} else if(n.muser.userType == 2) {
			usertype = '内部员工 ';
		}else if(n.muser.userType == 3) {
			usertype = '介绍人';
		}else if(n.muser.userType == 4) {
			usertype = '百夫长';
		}else if(n.muser.userType == 5) {
			usertype = '千夫长';
		}else if(n.muser.userType == 6) {
			usertype = '加盟商';
		}

		if(n.muser.mStatus==1){
			var status="禁用";
		}else if(n.muser.mStatus==2){
			var status="启用";
		}
		
		var id = n.muser.id;
		var parsentUserName="无",flag=false;
		if(n.muser.parsentUserName!=null && n.muser.parsentUserName!=""){
			parsentUserName=n.muser.parsentUserName;
			flag=true;
		}
		var userName=n.muser.userName==null?"":(n.muser.userName);
		var thislist =
			'<tr class="footable-even" style="display: table-row;">' +
			'<td class="footable-visible"><input type="checkbox" /></td>' +
			'<td class="footable-visible">' +'<a  class="btn"   href="muser1.html?id=' + n.muser.id+ '">'+n.muser.id+ '</a></td>' +
			'<td class="footable-visible">' + userName + '</td>' +
			'<td class="footable-visible">' + n.muser.phone+ '</td>' +
			'<td class="footable-visible">' + usertype + '</td>' +
		    '<td class="footable-visible">' + membertype + '</td>' +
			'<td class="footable-visible">' + new Date(n.muser.gmtDatetime).pattern("yyyy-MM-dd hh:mm:ss") + '</td>' +
			'<td class="footable-visible">' + mstatus + '</td>' +
			'<td class="footable-visible">' + parsentUserName+ '</td>' +
			'<td class="footable-visible"><img width="40px" height="40px" src="' + n.muser.qrCode + '"></td>' +
			'<td class="footable-visible footable-last-column">'+
			    '<a class=""  href="javascript:;" data-toggle="modal"   data-target="#chenge" onclick="store('+ id +','+flag+')" >添加上级</a>&nbsp;' +
			    '<a class="" name="查看" href="javascript:;" onclick="findSubUser(' + id + ')"  >查看下级</a>&nbsp;' +
			'</tr>';
		$('#thislist').append(thislist);
	});

		
}
	
	
	PageClick = function(pageclickednumber) {
		var userType = $('#userType').val();
		var memberType = $('#memberType').val();
		var phoneNumber = $('#phoneNumber').val();
		init(pageclickednumber,memberType,userType, phoneNumber,callback)
	}





function EnterSearchList() {
	var code = event.keyCode;
	  if(code == 13) {
		searchList();
	}

}

/**
 * 搜索
 */
function searchList() {

 var phoneNumber = $('#phoneNumber').val().trim();
  init(currentPage,"","", phoneNumber,callback)

}







/**
 * 添加上级
 * @param {Object} id
 */

function addRecomend(){
	var  recomend=$("#recomend").val();
	var userId=sessionStorage.getItem("userId");
	var parsentUserName=sessionStorage.getItem("parsentUserName");
	if(recomend==""){
		alert("请输入上级推荐人手机号或真实名称");
		return;
	}
	 var flag=true;
	if(parsentUserName=='true'){
		flag=confirm("该会员已有推荐人,是否确认更新？");
	}
	
	if(flag){
		$.ajax({
			url: urlcore + "/api/muser/addRecomendUser?userId="+userId+"&recomend="+recomend,
			type: "GET",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				if(data.success == true) {
					alert(data.msg)
					window.location.reload();
				} else {
					alert(data.msg);
				}
	
			},
			error: function() {
				alert("error");
			}
		});
	}
	
	
}

/**
 * 查看下级会员
 */
function findSubUser(id){

	$("#thislist").html("");
	$.ajax({
		url: urlcore + "/api/muser/selectUserTeamList?userId=" + id +"&current=" + currentPage,
		type: "GET",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
				callback(data.data);
					$("#pager").pager({
					pagenumber: currentPage,
					pagecount: data.data.pages,
					totalcount: data.data.total,
					buttonClickCallback: PageClick
				});
			} else {
				alert(data.msg);
			}

		},
		error: function() {
			alert("error");
		}
	});
	
}


function store(id,parsentUserName){
	
	sessionStorage.setItem("userId",id);
	sessionStorage.setItem("parsentUserName",parsentUserName);

}




